package springboot.demo.annotation;

import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;

public class NotProfileCondition implements Condition {
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        if (metadata.isAnnotated(NotProfile.class.getName())) {
            String[] notProfiles = (String[]) metadata.getAnnotationAttributes(NotProfile.class.getName()).get("value");
            String activeProfiles[] = context.getEnvironment().getActiveProfiles();
            for (String notProfile : notProfiles) {
                for (String activeProfile : activeProfiles) {
                    if (notProfile.trim().equals(activeProfile)) {
                        return false;
                    }
                }
            }
        }
        return true;
    }
}
